/**
 * author:Administrator
 * date:2016/3/14
 * desc: 整体 command 流程
 */

'use strict';
let co = require('co');
let FormatDate = require('../infrastructure/appUtil/FormatDate.js');

//
let roleService = require('../application/roleService.js');
let customerService = require('../application/customerCmdService.js');
let managerService = require('../application/managerCmdService.js');
let vendorService = require('../application/vendorCmdService.js');
let keeperService = require('../application/keeperCmdService.js');
let customerQueryService = require('../application/customerQueryService.js');

// load saga
let saga = require('../../ddd/domain/saga');

let _data = {
  mgr: {name: 'mgr1', pwd: 'mgr1'},
  cate: {name: 'cate1'},
  loc: {name: 'th'},

  vd: {name: 'vd1', pwd: 'vd1'},
  ware: {name: 'ware1', total: 11, price: '9.0'},
  shop: {name: 'shop1'},
  coupon: {maxCount: 10, money: 3, consumeLimit: 20},
  kp: {name: 'keeper1', pwd: 'keeper1'},

  cust: {name: 'cust1', pwd: 'cust1'},
};

co(function*() {

  /** manager cate/loca */
  yield roleService.registerManager(_data.mgr.name, _data.mgr.pwd);
  let mgr = yield roleService.login(_data.mgr.name, _data.mgr.pwd);
  //console.log('mgr=', mgr);
  let cate = yield managerService.createCategory(_data.cate.name);
  //console.log('cate=', cate);
  let loc = yield managerService.createLocation(_data.loc.name);
  //console.log('loc=', loc);

  /**
   * vendor ware/shop/coupon/keeper
   */
  yield roleService.registerVendor(_data.vd.name, _data.vd.pwd);
  let vd = yield roleService.login(_data.vd.name, _data.vd.pwd);
  //console.log('vd=', vd);
  let ware = yield vendorService.createWare(vd.id, _data.ware.name, cate.id, _data.ware.total, _data.ware.price);
  //console.log('ware=', ware);
  let shop = yield vendorService.createShop(vd.id, _data.shop.name, loc.id);
  //console.log('shop=', shop);
  let coupon = yield vendorService.createCouponMjs(vd.id, null, null, _data.coupon.maxCount, _data.coupon.consumeLimit, _data.coupon.money, null);
  //console.log('coupon=', coupon);
  let keeper = yield vendorService.createKeeper(vd.id, _data.kp.name, _data.kp.pwd);
  //console.log('keeper1=', keeper);
  yield vendorService.shopSetKeeper(vd.id, keeper.id, shop.id);
  yield vendorService.wareOnShelf(vd.id, ware.id, shop.id);

  /**
   * customer couponLog/order/pay
   */
  let cust = yield roleService.registerCustomer(_data.cust.name, _data.cust.pwd);
  cust = yield roleService.login(_data.cust.name, _data.cust.pwd);
  ////console.log('cust=', cust);

  let couponLog = yield customerService.applyCoupon(cust.id, coupon.id);
  yield delay();
  //console.log('couponLog aaa', couponLog);
  //console.log('coupon aaa', coupon);

  let order = yield customerService.addUnpaidOrder(cust.id, ware.id, 3);
  yield delay();
  //console.log('unpaid order=', order);
  //console.log('order createAt =', FormatDate.unix2Readable(order.createdAt));

  yield customerService.orderUseCoupon(cust.id, order.id, couponLog.id);
  yield delay();

  yield customerService.payOrder(order.id);
  yield delay();

  let ticket = yield customerQueryService.customerTicket(cust.id);
  console.log('ticket=', ticket);
  if (ticket && ticket.length > 0) ticket = ticket[0];
  //
  keeper = yield roleService.login(_data.kp.name, _data.kp.pwd);
  console.log('keeper2=', keeper);
  //
  let isUsed = yield keeperService.useTicket(keeper.id, shop.id, ticket.code);
  if (!isUsed) console.log('use ticket fail');
  console.log('**********all done***********');

  let t = yield delay(3);
  console.log('t=', t);
  console.log('bbbbbbbbbb done');


}).catch(function (err) {
  console.log(err.stack);
});

function delay(t) {
  t = ( t || 0.2 ) * 1000;
  return new Promise(function (fulfil, reject) {
    setTimeout(function () {
      fulfil(t)
    }, t);
  });
}

